package com.ccjq.jq167.dao.impl;

import com.ccjq.jq167.DbConnection;
import com.ccjq.jq167.dao.student_dao;
import com.ccjq.jq167.po.StudentPo;
import com.ccjq.jq167.uuid.Uuid;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class dao_impl implements student_dao {

    public List<StudentPo> select() {
        List<StudentPo> list = new ArrayList<StudentPo>();
        Connection con = null;
        PreparedStatement pre = null;
        ResultSet res = null;
        String select = "select id, name, sex, age, address, school from StudentTest ";
        try {
            con = DbConnection.getConnextion();
            pre = con.prepareStatement(select);
            res = pre.executeQuery();
            while (res.next()) {
                StudentPo u = new StudentPo();
                u.setId(res.getString("id"));
                u.setName(res.getString("name"));
                u.setSex(res.getString("sex"));
                u.setAge(res.getString("age"));
                u.setAddress(res.getString("address"));
                u.setSchool(res.getString("school"));
                list.add(u);

            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            DbConnection.close(res, pre, con);
        }
        return list;
    }

    public int insert(StudentPo student) {
        Uuid uid=new Uuid();
        String uuid=uid.uuid();
        int a = 0;
        Connection con = null;
        PreparedStatement pre = null;
        ResultSet res = null;
        String select = "insert into StudentTest values(?,?,?,?,?,?) ";
        try {
            con = DbConnection.getConnextion();
            pre = con.prepareStatement(select);
            pre.setString(1,uuid);
            pre.setString(2, student.getName());
            pre.setString(3, student.getSex());
            pre.setString(4, student.getAge());
            pre.setString(5, student.getAddress());
            pre.setString(6, student.getSchool());
            a = pre.executeUpdate();
            if (a > 0) {
                a = 1;
            } else {
                return a;
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            DbConnection.close(null, pre, con);
        }
        return a;
    }

    public int update(StudentPo student) {
        int a = 0;
        Connection con = null;
        PreparedStatement pre = null;
        ResultSet res = null;
        String select = "update  StudentTest  set name=?, sex=?,age=?,address=?,school=? where id=?";

        try {
            con = DbConnection.getConnextion();
            pre = con.prepareStatement(select);
            pre.setString(1,student.getName());
            pre.setString(2, student.getSex());
            pre.setString(3, student.getAge());
            pre.setString(4, student.getAddress());
            pre.setString(5, student.getSchool());
            pre.setString(6, student.getId());
            a = pre.executeUpdate();
            if (a > 0) {
                a = 1;
            } else {
                return a;
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            DbConnection.close(null, pre, con);
        }
        return a;
    }

    public int delete(StudentPo student) {
        int a = 0;
        Connection con = null;
        PreparedStatement pre = null;
        ResultSet res = null;
        String select = "delete StudentTest where id=?";
        System.out.println(select);
        try {
            con = DbConnection.getConnextion();
            pre = con.prepareStatement(select);
            pre.setString(1, student.getId());
            a = pre.executeUpdate();
            System.out.println(student.getId());
            if (a > 0) {
                a = 1;
            } else {
                return a;
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            DbConnection.close(null, pre, con);
        }
        return a;
    }

    public StudentPo chaxunzzz(StudentPo student) {
        StudentPo u=new StudentPo();
        Connection con = null;
        PreparedStatement pre = null;
        ResultSet res = null;
        String select = "select * from StudentTest where id=?";

        try {
            con = DbConnection.getConnextion();
            pre = con.prepareStatement(select);
            pre.setString(1, student.getId());
            res=pre.executeQuery();
            if(res.next())
            {
                u.setId(res.getString(1));
                u.setName(res.getString(2));
                u.setSex(res.getString(3));
                u.setAge(res.getString(4));
                u.setAddress(res.getString(5));
                u.setSchool(res.getString(6));
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            DbConnection.close(res, pre, con);
        }
        return u;
    }
}
